﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：王明亮
 * 创建时间：2022/12/7 14:23:07
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using AgoraChat;
using chatSDKApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Message = AgoraChat.Message;

namespace chatSDKApp.Forms.ChatRoom
{
    /// <summary>
    /// ChatSdkClass 的摘要说明
    /// </summary>
    public class ChatSdkClass : IChatManagerDelegate
    {
        //记录群聊列表的list
        public static  List<Group>  groupList = new List<Group>();
        //好友列表
        public static List<string> friendList = new List<string>();
        
        public ChatSdkClass()
        {
            GetGroupList();
            GetFriendsList();
        }

        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void CreateAccount(string username, string password)
        {
            SDKClient.Instance.CreateAccount(username, password, new CallBack(
        onSuccess: () => {
            LogHelper.Log("IM创建用户:", "创建用户成功");
            MessageBox.Show("创建用户成功！");
        },

        onError: (code, desc) => {
            LogHelper.Log("IM创建用户失败:", code + ":" + desc);
        }
    ));

        }
        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="username"></param>
        /// <param name="reason"></param>
        public void AddContact(string username,string reason ="对方请求添加好友")
        {
            SDKClient.Instance.ContactManager.AddContact(username, reason,   new CallBack(
                onSuccess: () =>
                {
                    LogHelper.Log("添加好友:", "添加好友申请成功");
                    MessageBox.Show("添加好友申请成功！");
                },
                onError: (code, desc) =>
                {
                    LogHelper.Log("添加好友申请失败:", code + ":" + desc);
                    MessageBox.Show("添加好友申请失败！");
                }
            ));
        }

        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="username"></param>
        public void DeleteContact(string username)
        {
            SDKClient.Instance.ContactManager.DeleteContact(username,false, new CallBack(
            onSuccess: () =>
            {
                LogHelper.Log("删除好友:", "删除好友申请成功");
                MessageBox.Show("删除好友申请成功！");
            },
            onError: (code, desc) =>
            {
                LogHelper.Log("删除好友申请失败:", code + ":" + desc);
                MessageBox.Show("删除好友申请失败！");
            }
             ));
        }

        /// <summary>
        /// 同意好友
        /// </summary>
        /// <param name="username"></param>
        public void AcceptInvitation(string username)
        {
            //同意好友请求。
            SDKClient.Instance.ContactManager.AcceptInvitation(username,   new CallBack(
                onSuccess: () =>
                {
                    LogHelper.Log("同意好友邀请:", $"添加{username} 成功！");
                },
                onError: (code, desc) =>
                {
                    LogHelper.Log("同意好友邀请:", $"添加{username} 失败！");
                }
            ));
        }

        /// <summary>
        /// 拒绝好友
        /// </summary>
        /// <param name="username"></param>
        public void DeclineInvitation(string username)
        {
            // 拒绝好友请求。
            SDKClient.Instance.ContactManager.DeclineInvitation(username,   new CallBack(
                onSuccess: () =>
                {
                    LogHelper.Log("拒绝好友邀请:", $"拒绝{username} 成功！");
                },
                onError: (code, desc) =>
                {
                    LogHelper.Log("拒绝好友邀请:", $"拒绝{username} 成功！");
                }
            ));
        }



        /// <summary>
        /// 从服务端获取好友
        /// </summary>
        public void GetFriendsList()
        {
            try
            {
                SDKClient.Instance.ContactManager.GetAllContactsFromServer(new ValueCallBack<List<string>>(
              onSuccess: (list) =>
              {
                  friendList = list;

              }, onError: (code, desc) =>
              {
                  LogHelper.Log("IM获取用户好友列表失败:", code + ":" + desc);
              }));

            }
            catch (Exception ex)
            {
                LogHelper.Log("IM-GetAllContacts:", ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 获取群聊列表
        /// </summary>
        public void GetGroupList()
        {

            try
            {
               // List<AgoraChat.Group> groupList = SDKClient.Instance.GroupManager.GetJoinedGroups();
                SDKClient.Instance.GroupManager.FetchJoinedGroupsFromServer(1, 200,false,false, new ValueCallBack<List<AgoraChat.Group>>(
             onSuccess: (groups) =>
             {
                 //群聊列表
                 groupList = groups;

             }, onError: (code, desc) =>
             {
                 LogHelper.Log("IM获取用户群聊信息失败:", code + ":" + desc);
             }));
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        /// <summary>
        /// 创建群聊
        /// </summary>
        /// <param name="members"></param>
        /// <param name="groupName"></param>
        /// <param name="groupDesc"></param>
        public void GreateGroup(List<string> members,string groupName,string groupDesc)
        {
            GroupOptions option = new GroupOptions(GroupStyle.PublicOpenJoin);// 公开群，所有群成员均可以邀请人进群；
            option.MaxCount = 100;
            option.InviteNeedConfirm = false;//邀请进群需要确认
            SDKClient.Instance.GroupManager.CreateGroup(groupName, option, groupDesc, members,null, new ValueCallBack<AgoraChat.Group>(
                onSuccess: (group) => {
                    //建立的群聊
                    LogHelper.Log("IM建立群聊:",$"建群:{group.Name}成功");
                    MessageBox.Show("建立群聊成功！");
                },
                onError: (code, error) => {
                    LogHelper.Log("IM建立群聊失败:", code + ":" + error);
                    MessageBox.Show("建立群聊失败！");
                }
            ));

        }

        /// <summary>
        /// 加入群聊
        /// </summary>
        public void JoinGroup(string groupId)
        {
            try
            {
                // 申请加入群组
                SDKClient.Instance.GroupManager.JoinPublicGroup(groupId, new CallBack(
                    onSuccess: () =>
                    {
                        LogHelper.Log("JoinPublicGroup", $"加群:{groupId}成功！");
                        MessageBox.Show("加群成功");
                    },
                    onError: (code, desc) =>
                    {
                        LogHelper.Log("JoinPublicGroup", $"加群:{groupId}失败！");
                        MessageBox.Show("加群成功");
                    }
                ));
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 退群
        /// </summary>
        /// <param name="groupId"></param>
        public void LeaveGroup(string groupId)
        {
            SDKClient.Instance.GroupManager.LeaveGroup(groupId, new CallBack(
        onSuccess: () =>
        {
            LogHelper.Log("DestroyGroup", $"退群:{groupId}成功！");
            MessageBox.Show("退群成功");
        },
        onError: (code, desc) =>
        {
            LogHelper.Log("DestroyGroup", $"退群:{groupId}失败！");
            MessageBox.Show("退群失败");
        }
    ));

        }

        /// <summary>
        /// 解散群
        /// </summary>
        public void DestroyGroup(string groupId)
        {
            SDKClient.Instance.GroupManager.DestroyGroup(groupId, new CallBack(
               onSuccess: () =>
               {
                   LogHelper.Log("DestroyGroup",$"解散群:{groupId}成功！");
                   MessageBox.Show("解散群成功");
               },
               onError: (code, desc) =>
               {
                   LogHelper.Log("DestroyGroup", $"解散群:{groupId}失败！");
                   MessageBox.Show("解散群成功");
               }
           ));

        }

        /// <summary>
        /// 获取群详细信息
        /// </summary>
        /// <param name="groupId"></param>
        public void getGroupSpec(string groupId)
        {
            // 根据群组 ID 从本地获取群组详情。
            //Group group = SDKClient.Instance.GroupManager.GetGroupWithId(groupId);

            // 根据群组 ID 从服务器获取群组详情。
            SDKClient.Instance.GroupManager.GetGroupSpecificationFromServer(groupId, new ValueCallBack<Group>(
                onSuccess: (group) => {

                },
                onError: (code, desc) =>
                {

                }
            ));

        }

        /// <summary>
        /// 服务端拉取聊天记录
        /// </summary>
        /// <param name="conversationId"></param>
        /// <param name="conversationType"></param>
        /// <param name="startId"></param>
        /// <param name="pageSize"></param>
        public void FetchHistoryMessagesFromServer( string conversationId,  ConversationType conversationType  = ConversationType.Chat, string startId = null , int pageSize =20)
        {
            SDKClient.Instance.ChatManager.FetchHistoryMessagesFromServer(conversationId, conversationType, startId, pageSize, MessageSearchDirection.UP, new ValueCallBack<CursorResult<Message>>(
              //获取历史消息成功。
              //result 为 CursorResult<Message> 类型。
              onSuccess: (result) =>
              {

              },

              //获取历史消息失败。
              onError: (code, desc) =>
              {

              }
            ));
        }

        /// <summary>
        /// 获取群组成员
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="pageSize"></param>
        /// <param name="cursor"></param>
        public void getGroupMembers(string groupId,int pageSize = 200,string cursor ="")
        {
            SDKClient.Instance.GroupManager.GetGroupMemberListFromServer(groupId, pageSize, cursor,   new ValueCallBack<CursorResult<string>>(
      onSuccess: (result) =>
      {
    
      },
      onError: (code, desc) =>
      {
     
      }
         ));
        }



        #region 实现接口方法
        public void MessageReactionDidChange(List<MessageReactionChange> list)
        {
            throw new NotImplementedException();
        }

        public void OnCmdMessagesReceived(List<Message> messages)
        {
            throw new NotImplementedException();
        }

        public void OnConversationRead(string from, string to)
        {
            throw new NotImplementedException();
        }

        public void OnConversationsUpdate()
        {
            throw new NotImplementedException();
        }

        public void OnGroupMessageRead(List<GroupReadAck> list)
        {
            throw new NotImplementedException();
        }

        public void OnMessagesDelivered(List<Message> messages)
        {
            throw new NotImplementedException();
        }

        public void OnMessagesRead(List<Message> messages)
        {
            throw new NotImplementedException();
        }

        public void OnMessagesRecalled(List<Message> messages)
        {
            throw new NotImplementedException();
        }

        public void OnMessagesReceived(List<Message> messages)
        {
            throw new NotImplementedException();
        }

        public void OnReadAckForGroupMessageUpdated()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
